/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SaleManager.Category.Logic;

import Data.DAO.IBookStore;
import Data.DAO.IDataStore;
import data.DAO.ICategoryStore;
import data.pojo.Book;
import data.pojo.Category;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 *
 * @author Kite
 */
public class AddCategoryManage {
    
    private List<Category> newList = new ArrayList<Category>();
    private List<Book> searchList = new ArrayList<Book>();
    private IDataStore categoryDS = new ICategoryStore();   
    private IDataStore bookDS = new IBookStore();   
    private ArrayList<Category> allCategories;
    
    
    public AddCategoryManage() {
        allCategories = (ArrayList<Category>) categoryDS.getObjects();
    }
    
    public Vector<List> searchTableVector(String key, String keywords) {
         searchList = bookDS.getObjectsByAttr(key, ".*" + keywords + ".*");
        return new Vector<List>(toBookTable(searchList));
    }
    
    public Vector<List> bookTableVector(List<Book> booklist) {
        return new Vector<List>(toBookTable(booklist));
    }
    
    public List<Category> getNewList() {
        return this.newList;
    }

    public boolean addToNewList(Category category) {
        if (this.isValid(category)) {
            this.newList.add(category);
            return true;
        } else {
            return false;
        }
    }

    private boolean isValid(Category category) {
        if (!this.categoryDS.searchObjectsByAttr("name", String.valueOf(category.getName())).isEmpty()) {
            return false;
        }
        for (Category c : newList) {
            if (c.getName() == category.getName()) {
                return false;
            }
        }
        return true;
    }
    
    public boolean bookValid(Category c,Book b){
        
        if(c.getBookList()==null){
            List<Book> bList = new ArrayList<Book>();
            c.setBookList(bList);
            return true;
        }
        for(Book book: c.getBookList()){
            if(book.getISBN()==b.getISBN()){
                return false;
            }
        }
        return true;
    };

    public Category getByIndex(int index) {
        return this.newList.get(index);
    }
    
    public Book getBySearch(int index) {
        return this.searchList.get(index);
    }

    public boolean setByIndex(int index, Category category) {
        if (this.isValid(category)) {
            this.newList.set(index, category);
            return true;
        } else {
            return false;
        }
    }

    public void removeFromNewList(int index) {
        this.newList.remove(index);
    }

    public boolean commitNewList() {
        if (this.newList.isEmpty()) {
            return false;
        } else {
            this.categoryDS.add(newList);
            newList = new ArrayList<Category>();
            return true;
        }
    }
    
    public Vector<List> categoryTableVector() {
        return new Vector<List>(toCategoryTable(newList));
    }

    public Vector<Vector<String>> toCategoryTable(List list) {
        try {
            Vector<Category> seList = new Vector(list);
            Vector<Vector<String>> reList = new Vector<Vector<String>>();
            for (int i = 0; i < seList.size(); i++) {
                reList.add((Vector<String>) toCategoryStringArray(seList.get(i)));
            }
            return reList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List toCategoryStringArray(Category category) {
        Vector<String> list = new Vector<String>();
        list.add(category.getName());//name
        return list;
    }
    


    public Vector<Vector<String>> toBookTable(List list) {
        try {
            Vector<Book> seList = new Vector(list);
            Vector<Vector<String>> reList = new Vector<Vector<String>>();
            for (int i = 0; i < seList.size(); i++) {
                reList.add((Vector<String>) toBookStringArray(seList.get(i)));
            }
            return reList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List toBookStringArray(Book book) {
        Vector<String> list = new Vector<String>();
        list.add(book.getISBN());//ISBN
        list.add(book.getTitle());//title
        list.add(book.getAuthor());//Author
        list.add(((Double) book.getPrice()).toString());//price     
        list.add(String.valueOf(book.getPublisher()));//publicsher
        list.add("");//publicationDate
        return list;
    }
    
}
